<?php
/**
 * 用户订单管理
 */
namespace V3\Controller;

use Think\Controller;

class OrderController extends ApiCommonController
{
	
	//提交单个商品
	    public function addorder()
    {	
      	$data['user_id']=trim(I('post.user_id'));					
		$data['code_money'] = trim(I('post.code_money'));	  
		$data['pay_type'] = trim(I('post.pay_type'));		
        $data['r_username'] = trim(I('post.r_username'));
        $data['r_phone'] = trim(I('post.r_phone'));
        $data['r_addr'] = trim(I('post.r_addr'));
		$data['product_id']=trim(I('post.product_id'));
		$data['product_count'] = trim(I('post.product_count'));				
        $data['product_color'] = trim(I('post.product_color'));
        $data['product_guige'] = trim(I('post.product_guige'));		
		
		M('Order')->startTrans();	
		if(in_array('',$data)){
					$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '缺少必要参数',
			));
		}else{
			
			$data['code']=time().'_'.mt_rand();	
		
			$data['created_time'] = time();
        	$data['modified_time'] = time();
			$data['pay_status'] = 1;//支付状态:1已支付 2未支付
			$data['code_status'] = 2;//订单状态: 1全部 2待付款 3待发货 4待收货5交易完成6交易关闭
			$order=M('Order')->add($data);	
				
				if($order){			
				$datagoods=M('Goods')->where('id='.$data['product_id'])->find();	
				
				$name = M('Movie_v3')->getFieldById($datagoods['movie_id'],'name');	
						
				$data['order_id'] = $order;												
				$data['product_brand'] = $datagoods['brand'];					
                $data['product_id'] =$datagoods['id'];
                $data['product_title'] = $datagoods['name'];				
				$data['product_desc'] = $datagoods['base_info'];
				$data['product_price'] = $datagoods['sale_price'];	
				$data['product_img'] = $datagoods['square_photo'];				               							
                $data['jxs_id'] = $datagoods['movie_id'];                             
                $data['jxs_title'] = $name ;               
				$data['created'] = time();
                $data['modified'] = time();   				
				$re = M('Orderdetial')->add($data); 			
				if ($re) {
					
					$num=M('Goods')->getFieldById($data['product_id'],'number');
				
					if($num>=$data['product_count']){					
						M('Order')->commit();
						M('Goods')->where('id='.$data['product_id'])->setDec('number',$data['product_count']);	
						$code=$data['code'];
						$return_data = array(
			 			'status'	=> 1,
			 			'info'	=> '提交成功！',
			 			'data'=>$code,
					 );					
					}else{
					
						M('Order')->rollback();
	
					$return_data = array(
			 		'status'	=> 2,
			 		'info'	=> '库存不足！',
					 );
					}
                } else {
                  M('Order')->rollback();
				  $return_data = array(
			 	'status'	=> 2,
			 	'info'	=> '订单提交失败',
					 );
				  
                }	
                		       		
			}		
			}
		
							
	 $this->ajaxReturn($return_data);		 
    }
//提交购物车商品
	public function addcart() {	  
	
		$uid = trim(I('post.user_id'));
		$ids = trim(I('post.cart_id'));
		$pay_type = trim(I('pay_type'));
		$r_username = trim(I('r_username'));
		$r_phone =trim(I('r_phone'));
		$r_addr = trim(I('r_addr'));
		
		if(empty($uid)||empty($ids)){
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '缺少必要参数',
			));
		}
		
		$cartlist = M('Cart')->where(array('user_id'=>$uid,'id'=>array('in',explode(',',$ids))))->select(); 
		
		
		$datalist = array();
		foreach($cartlist as $k => $cart){
			$tar = $cart;
			$datalist[$cart['jxs_id']][] = $tar;
		}

		
		$error = false;
		$errorMes='';
		M('Order')->startTrans();
		
		foreach($datalist as $k => $cart){
			
			$oData = array();
			$oData['user_id']=$uid;		
			$oData['code']=time().'_'.mt_rand();	
	    	$oData['code_status'] = 2;
			$oData['pay_type'] = $pay_type;
			$oData['pay_status'] = 1;
            $oData['r_username'] = $r_username;
            $oData['r_phone'] = $r_phone;
            $oData['r_addr'] = $r_addr;
			$oData['created_time'] = time();
            $oData['modified_time'] = time();	
			
			$totalmoney = 0;
			foreach($cart as $ct){
				$totalmoney += $ct['product_price']*$ct['product_count'];
			}			
			$oData['code_money'] = $totalmoney;
			
			
			
			
			
			$orderid = M('Order')->add($oData); 
			
			if($orderid){
				
				foreach($cart as $ct){
					$odData = array(); 
					$odData['order_id'] = $orderid;	
					$odData['product_id'] = $ct['product_id'];  
					$datagoods=M('Goods')->where('id='.$odData['product_id'])->find();	
					
					$name = M('Movie_v3')->getFieldById($datagoods['movie_id'],'name');	
					
					
					$odData['user_id'] = $uid;									
					$odData['product_brand'] = $datagoods['brand'];					
	                $odData['product_id'] =$datagoods['id'];
	                $odData['product_title'] = $datagoods['name'];				
					$odData['product_desc'] = $datagoods['base_info'];
					$odData['product_price'] = $datagoods['sale_price'];	
					$odData['product_img'] = $datagoods['square_photo'];
	                $odData['product_count'] = $ct['product_count'];				
	                $odData['product_color'] = $ct['product_color'];	
	                $odData['product_guige'] = $ct['product_guige'];							
	                $odData['jxs_id'] = $datagoods['movie_id'];                             
	                $odData['jxs_title'] = $name ;               
					$odData['created'] = time();
	                $odData['modified'] = time(); 	
 
						if($datagoods['number']<$odData['product_count']){
							$error=true;
							$errorMes=$datagoods['number'].'库存不足'.$oData['product_count'].'件';
							break;
						}	
					$rs = M('Orderdetial')->add($odData);
					if(!$rs) {
						
						$error = false;
						break;
					}else{
						
						 $product_id=M('Cart')->getFieldById($ids,'id');
						$num=M('Goods')->getFieldById($product_id,'number');
						
						
					
						M('Goods')->where('id='.$odData['product_id'])->setDec('number',$odData['product_count']);
					}
				}
			}else{
				$error = true;
				break;
			}
		}
		
		if(!$error){
			M('Order')->commit();	
			
			M('Cart')->where(array('user_id'=>$uid,'id'=>array('in',explode(',',$ids))))->delete();
			
			
			$return_data = array(
			 	'status'	=> 1,
			 	'info'	=> '订单提成功！',
			 	'data'=>$code,
			 
			 	
			 );
		}else{
			
			M('Order')->rollback();
			$return_data = array(
			 	'status'	=> 2,
			 	'info'	=> '库存不足！',
			 );
			 if(!empty($reeorMes))$return_data['info']=$errorMes;
		}
		$this->ajaxReturn($return_data);
		exit();
	}


		//查看订单
	public function getlist()
    {	
      	$data['user_id']=trim(I('get.user_id'));
		$code_status=trim(I('get.code_status'));
	

		if($code_status!=1){
					
			$where['code_status']=$code_status;		
			$where['status']=1;
			$where['user_id']=$data['user_id'];
		
		$od=M('Order')->where($where)->order('code_status,created_time desc')->select();
		$olist=array();
		foreach ($od as $key => $value) {
			$odata=array();
			$odata['id']=$value['id'];
			$odata=$value;
			$odd=M('Orderdetial')->where('order_id='.$odata['id'])->select();
				foreach ($odd as $k => $v) {
					$oddata=array();
					$oddata=$v;
			$odata['data'][]=$oddata;
					$odata['name']=$v['jxs_title'];
					$odata['img']=	M('Movie_v3')->getFieldById($v['jxs_id'],'roundness_photo');
				}
			
			$olist[]=$odata;		
		}


			if($od){
					$return_data['status']='1';
		            $return_data['info']='成功';
					$return_data['data']=$olist;
				
			}else{
					$return_data['status']='1';
		            $return_data['info']='成功';
					$return_data['data']=array();
			}
		
				
			}else{
				
		$w['user_id']=$data['user_id'];
		
		$od=M('Order')->where($w)->order('code_status,created_time desc')->select();
		
		$olist=array();
		foreach ($od as $key => $value) {
			$odata=array();
			$odata['id']=$value['id'];
			$odata=$value;
			$odd=M('Orderdetial')->where('order_id='.$odata['id'])->select();
				foreach ($odd as $k => $v) {
					$oddata=array();
					$oddata=$v;
					$odata['name']=$v['jxs_title'];
					$odata['img']=	M('Movie_v3')->getFieldById($v['jxs_id'],'roundness_photo');
			$odata['data'][]=$oddata;		
				}
			
			$olist[]=$odata;		
		}

			if($od){
					$return_data['status']='1';
		            $return_data['info']='成功';
					$return_data['data']=$olist;
				
			}else{
					$return_data['status']='2';
		            $return_data['info']='失败';
			}
			}
				

 $this->ajaxReturn($return_data);
 }
	

	//修改地址
		    public function editradd()
    {	
   		$id=trim(I('post.id'));
		$r_username=trim(I('post.r_username'));
		$r_addr=trim(I('post.r_addr'));
		$r_phone=trim(I('post.r_phone'));
		

		$data = array(
			'r_username'=>$r_username,
			'r_addr' => $r_addr,
			'r_phone' => $r_phone,
			'modified_time' =>time(),
		);
		
		if($id){
			$re=M('Order')->where('id='.$id)->save($data);
			if($re){
		               $return_data['status']='1';
		                $return_data['info']='修改成功!';
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='修改失败!';
            }	
				
				  	$this->ajaxReturn($return_data);		
		}

		 
		 
    }	
	//静默取消
	public function confup(){	
		$id=trim(I('post.id'));	
		
					
			$created_time=M('Order')->where('id='.$id)->find();	
			
			$day=24*60*60;
			$conftime=$day+$created_time['created_time']; //取消日期		
			$time=time();
			
			if($conftime<=$time){
						$data = array(
						'status'	=>	3,
						'modified_time' =>time(),
						);
					$re=M('Order')->where('id='.$id)->save($data);	
					if($re){
						
						$gdata=M('Orderdetial')->where('order_id='.$id)->find();						
						M('Goods')->where('id='.$gdata['product_id'])->setInc('number',$gdata['product_count']);
						$return_data['status']='1';
		                $return_data['info']='成功';		
						
					}else{		              
		               	$return_data['status']='2';
		                $return_data['info']='失败！';					
           				 }	
					
		        }else{
		               
		               $uptime=$conftime-$time;
				
		               	$return_data['status']='2';
		                $return_data['info']='失败！';
						$return_data['data']=$uptime;
						
            }	
			
			
		
		  	$this->ajaxReturn($return_data);
    }
	
	
	
	
	
	
	
		
	//取消订单-删除    
	public function editrstate()
    {
    		
		$id=trim(I('post.id'));
		$tye=trim(I('post.tye'));	
		if( $tye == 1){
			$data = array(
			'code_status'=>6,
			'status'	=>	2,
			'modified_time' =>time(),
			
		);
		
			$re=M('Order')->where('id='.$id)->save($data);
		
				if($re){
					$gdata=M('Orderdetial')->where('order_id='.$id)->find();						
					M('Goods')->where('id='.$gdata['product_id'])->setInc('number',$gdata['product_count']);
		               $return_data['status']='1';
		                $return_data['info']='删除成功!';
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='删除失败!';
            }			
		}
		if($tye==2){
			$data = array(
			'code_status'=>6,
			'status'=>	3,
			'modified_time' =>time(),
			
		);
			$re=M('Order')->where('id='.$id)->save($data);
			if($re){
				$gdata=M('Orderdetial')->where('order_id='.$id)->find();						
				M('Goods')->where('id='.$gdata['product_id'])->setInc('number',$gdata['product_count']);
		               $return_data['status']='1';
		                $return_data['info']='取消成功!';
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='取消失败!';
            }			
		}
		 
		  	$this->ajaxReturn($return_data);
    }
	//查看物流
	public function Check()
    {	
		$id=trim(I('get.id'));			
			$re=M('Order')->where('id='.$id)->field('wuliu_company,wuliu_code')->select();							
		if($re){
		          $return_data['status']='1';
		          $return_data['info']='成功!';
				  $return_data['data']=$re;
						
		        }else{
		          $return_data['status']='2';
		          $return_data['info']='失败!';
            }			
	 
		  	$this->ajaxReturn($return_data);
    }
		
	
		
	//确认收货
	public function conf()
    {	
		$id=trim(I('post.id'));	
		if($id){
			$data = array(
			'code_status'	=>6,
			'modified_time' =>time(),			
		);
			$re=M('Order')->where('id='.$id)->save($data);
			if($re){
		               $return_data['status']='1';
		                $return_data['info']='成功!';
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='失败!';
            }				
		}		
		  	$this->ajaxReturn($return_data);
    }
	//申请退款
	public function onrefund()
    {	
		$id=trim(I('post.id'));	
		
		if($id){
			$data = array(
			'code_status'	=>5,
			'refund_status'	=>1,
			'modified_time' =>time(),			
		);
			$re=M('Order')->where('id='.$id)->save($data);
			if($re){
		               $return_data['status']='1';
		                $return_data['info']='成功!';
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='失败!';
            }			
		}	
		  	$this->ajaxReturn($return_data);
    }

	
		
	//延长收货
	public function extended()
    {	
		$id=trim(I('post.id'));			
			$wuliu_time=M('Order')->where('id='.$id)->find();	
			$t1=$wuliu_time['wuliu_time'];
			$t2=7*24*60*60;
			$cs=$t2*2;			
			$t3=15*24*60*60;
			$t5=time();
			
			if($wuliu_time['ext_time']){
				$e1=$wuliu_time['ext_time'];	
				$tye=$e1-$t1;
				$ext_time=$e1+$t2;//延长第二次
				$t4=$e1+$t3;					
				$t6=$t4-$t5;
				$t7=$t6+$t2;
			}else{
				$t4=$t1+$t3;					
				$t6=$t4-$t5;
				$t7=$t6+$t2;
				$tye=0;
				$ext_time=$t1+$t2;//延长一次时间	
			}
		
			if($tye<$cs){									
			$data = array(
			'ext_time'=>$ext_time,
			'modified_time' =>time(),			
			);
			$re=M('Order')->where('id='.$id)->save($data);
			if($re){
		               $return_data['status']='1';
		                $return_data['info']='成功!';
						$return_data['data']=$t7;
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='失败!';
            }		
			}else{
				
				 $return_data['status']='2';
		         $return_data['info']='已延长俩次';
			}
			
		  	$this->ajaxReturn($return_data);
    }
	
	//静默收货
	public function conftime()
    {	
		$id=trim(I('post.id'));			
		if($id){			
			$wuliu_time=M('Order')->where('id='.$id)->find();	
				 if ($wuliu_time['ext_time']){
			 	$time1=$wuliu_time['ext_time'];
			 }else{
			 	$time1=$wuliu_time['wuliu_time'];
				
			 }				
			$time=time();
			$day=15*24*60*60;
			$shou=$time1+$day;
			$sy=$shou-$time;
			
			if($time>=$shou){		
			$data = array(
			'code_status'=>6,
			'modified_time' =>time(),			
		);
			$re=M('Order')->where('id='.$id)->save($data);
				$return_data['status']='1';
		        $return_data['info']='交易完成';
			}
			else{
				$return_data['status']='2';
		        $return_data['info']='时间没到！';
				$return_data['data']=$sy;
				
			}			
		} 
		  	$this->ajaxReturn($return_data);
	}	
	//单个获取订单取消和收货时间
	public function gettime()
    {	
		$id=trim(I('get.id'));	
		$code_status=trim(I('get.code_status'));
					
		if($code_status==2){
			$gt=M('Order')->where('id='.$id)->find();	
			
			
			$time=time();
			$up=24*60*60;
			$add=$gt['created_time']+$up;
			$rtime=$add-$time;
			
			if($gt){
		               $return_data['status']='1';
		                $return_data['info']='成功!';
						$return_data['data']=$rtime;
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='失败!';
            }	
			
		} 
		if($code_status==4){
			
			$wuliu_time=M('Order')->where('id='.$id)->find();	
			 if ($wuliu_time['ext_time']){
			 	$time1=$wuliu_time['ext_time'];
			 }else{
			 	$time1=$wuliu_time['wuliu_time'];
				
			 }		
			$time3=15*24*60*60;			
			$time4=$time1+$time3;
			$time5=time();		
			$time6=$time4-$time5;
			if($wuliu_time){
		               $return_data['status']='1';
		                $return_data['info']='成功!';
						$return_data['data']=$time6;
		        }else{
		                $return_data['status']='2';
		                $return_data['info']='失败!';
            }	
		}
		  	$this->ajaxReturn($return_data);
	}
	
}
